package controllayer;

import modellayer.*;

/**
 * The Class OrderController.
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class OrderController {

	/** The p ctrl. */
	private PersonController pCtrl;

	/** The i ctrl. */
	private ItemController iCtrl;

	/** The o con. */
	private OrderContainer oCon;

	/**
	 * Instantiates a new order controller.
	 */
	public OrderController() {
		pCtrl = new PersonController();
		oCon = OrderContainer.getInstance();
		iCtrl = new ItemController();
	}

	/**
	 * Find order.
	 *
	 * @param orderID
	 *            the order id
	 * @return the order
	 */
	public Order findOrder(int orderID) {
		return oCon.findOrder(orderID);
	}

	/**
	 * Sets the order id.
	 */
	public void setOrderID() {
		oCon.setOrderID();
	}

	/**
	 * Removes the order.
	 *
	 * @param orderID
	 *            the order id
	 */
	public void removeOrder(int orderID) {
		oCon.removeOrder(orderID);
	}

	/**
	 * Removes the part order.
	 *
	 * @param tempO
	 *            the temp o
	 * @param type
	 *            the type
	 */
	public void removePartOrder(Order tempO, String type) {
		oCon.removePartOrder(tempO, type);
	}

	/**
	 * Creates the order.
	 *
	 * @return the order
	 */
	public Order createOrder() {
		return oCon.createOrder();
	}

	/**
	 * Sets the salesman.
	 *
	 * @param o
	 *            the o
	 * @param s
	 *            the s
	 */
	public void setSalesman(Order o, Salesman s) {
		oCon.setSalesman(o, s);
	}

	/**
	 * Sets the customer.
	 *
	 * @param o
	 *            the o
	 * @param c
	 *            the c
	 */
	public void setCustomer(Order o, Customer c) {
		oCon.setCustomer(o, c);
	}

	/**
	 * Find salesman.
	 *
	 * @param id
	 *            the id
	 * @return the person
	 */
	public Person findSalesman(int id) {
		return pCtrl.findSalesman(id);
	}

	/**
	 * Find customer.
	 *
	 * @param phone
	 *            the phone
	 * @return the person
	 */
	public Person findCustomer(String phone) {
		return pCtrl.findCustomer(phone);
	}

	/**
	 * Find business customer.
	 *
	 * @param cvr
	 *            the cvr
	 * @return the person
	 */
	public Person findBusinessCustomer(String cvr) {
		return pCtrl.findBusinessCustomer(cvr);
	}

	/**
	 * Adds the item.
	 *
	 * @param i
	 *            the i
	 * @param amount
	 *            the amount
	 * @param o
	 *            the o
	 */
	public void addItem(Item i, Integer amount, Order o) {
		if (iCtrl.getAmount(i) < amount) {
			throw new NotEnoughItemsException(
					"Ikke nok varer af denne type!\nAntal varer på lageret: "
							+ iCtrl.getAmount(i));
		}
		PartOrder tempPO = new PartOrder(i, amount);
		o.addPartOrder(tempPO);
	}

	/**
	 * Removes the item.
	 *
	 * @param itemID
	 *            the item id
	 * @param o
	 *            the o
	 */
	public void removeItem(int itemID, Order o) {
		o.removeItem(itemID);
	}

	/**
	 * Find item.
	 *
	 * @param itemID
	 *            the item id
	 * @return the item
	 */
	public Item findItem(int itemID) {
		return iCtrl.findItem(itemID);
	}

	/**
	 * Update amounts.
	 *
	 * @param o
	 *            the o
	 */
	public void updateAmounts(Order o) {
		o.updateAmounts();
	}
}